package com.xiyan.base.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiyan.base.entity.User;
import com.xiyan.base.service.IUserService;
import com.xiyan.base.utils.ResponseResult;
import com.xiyan.base.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ruiyanglee
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/api/user")
@Api(value = "User api", tags = "用户接口")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource
    private IUserService userService;

    @ApiOperation("用户信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list() {
        try {
            List<User> users = userService.list();
            return ResponseResult.ok().data(users);
        } catch (Exception e) {
            log.error("list interface exception info {}", e.getMessage());
            return ResponseResult.error().message("用户列表请求异常");
        }
    }

    @ApiOperation("用户信息")
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult findById(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            User user = userService.getById(id);
            return ResponseResult.ok().data(user);
        } catch (Exception e) {
            log.error("findById interface exception info {}", e.getMessage());
            return ResponseResult.error().message("用户信息请求异常");
        }
    }

    @ApiOperation("修改用户信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult update(@RequestBody User user) {
        try {
            if (ObjectUtil.isNull(user)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("user参数为空");
            }
            userService.saveOrUpdate(user);
            System.out.println(user.toString());
            return ResponseResult.ok().message("用户修改成功");
        } catch (Exception e) {
            log.error("add interface exception info {}", e.getMessage());
            return ResponseResult.error().message("修改失败");
        }
    }

    @ApiOperation("删除用户信息")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseResult del(@RequestParam String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            userService.removeById(id);
            return ResponseResult.ok().message("用户删除成功");
        } catch (Exception e) {
            log.error("del interface exception info {}", e.getMessage());
            return ResponseResult.error().message("用户删除失败");
        }
    }
}
